// *********************************************************
//      Copyright (c) Kosta Gaitanis
//      All rights reserved
//
//      k.gaitanis@gmail.com
//
// *********************************************************

#include <QVBoxLayout>
#include <QEvent>
#include <QDebug>

#include "CollapsibleWidget.h"

CollapsibleWidget::CollapsibleWidget(QWidget *parent) :
    QFrame(parent),
    m_isCollapsed(true),
    m_mainLayout(new QVBoxLayout()),
    m_collapseButton(new QLabel()),
    m_title(new QLabel()),
    m_widget(0),
    m_timeLine(new QTimeLine(300, this))
{
    m_collapseButton->installEventFilter(this);
    if (m_isCollapsed) {
        m_collapseButton->setPixmap(QPixmap(":/icons/ArrowRight"));
    } else {
        m_collapseButton->setPixmap(QPixmap(":/icons/ArrowDown"));
    }
    m_collapseButton->setMaximumSize(12,12);
    m_collapseButton->setMaximumSize(12,12);
    m_collapseButton->setScaledContents(true);

    QWidget *titleWidget = new QWidget();
    QHBoxLayout *titleLayout = new QHBoxLayout(titleWidget);
    titleLayout->addWidget(m_collapseButton);
    titleLayout->addWidget(m_title);
    titleLayout->setContentsMargins(5,2,5,2);

    m_mainLayout->addWidget(titleWidget);
    setLayout(m_mainLayout);

    m_timeLine->setFrameRange(0, 100);
    connect(m_timeLine, SIGNAL(frameChanged(int)), this, SLOT(onFrameChanged(int)));

    QFrame* line = new QFrame();
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Sunken);
    m_mainLayout->addWidget(line);

    m_mainLayout->setContentsMargins(0,0,0,0);
    m_mainLayout->setSpacing(0);
}


void CollapsibleWidget::setTitle(const QString &title)
{
    m_title->setText(title);
}

void CollapsibleWidget::setWidget(QWidget *widget)
{
    if (m_widget) {
        m_mainLayout->removeWidget(m_widget);
    }

    m_widget = widget;

    if (m_widget) {
        m_widget->setMinimumHeight(0);
        m_mainLayout->insertWidget(1, m_widget);
        if (m_isCollapsed) {
            m_widget->setMaximumHeight(0);
        }
    }
}

bool CollapsibleWidget::isCollapsed() const
{
    return m_isCollapsed;
}

void CollapsibleWidget::toggleCollapsed()
{
    m_isCollapsed = !m_isCollapsed;

    if (m_widget) {
        m_sizeHint = m_widget->sizeHint();
    } else {
        m_sizeHint = QSize(0,0);
    }
    if (m_isCollapsed) {
        m_timeLine->setDirection(QTimeLine::Backward);
        m_collapseButton->setPixmap(QPixmap(":/icons/ArrowRight"));
    } else {
        m_timeLine->setDirection(QTimeLine::Forward);
        m_collapseButton->setPixmap(QPixmap(":/icons/ArrowDown"));
    }

    if (m_timeLine->state() != QTimeLine::Running) {
        m_timeLine->start();
    }
}

void CollapsibleWidget::setCollapsed(bool collapsed)
{
    if (m_isCollapsed != collapsed) {
        toggleCollapsed();
    }
}

bool CollapsibleWidget::eventFilter(QObject *o, QEvent *e)
{
    Q_UNUSED(o);
    if (e->type() == QEvent::MouseButtonPress) {
        toggleCollapsed();
    }
    return false;
}

void CollapsibleWidget::onFrameChanged(int frame)
{
    if (!m_widget) {
        return;
    }
    setFocus();
    m_widget->setMaximumHeight(m_sizeHint.height() / 100.0 * frame);
}

